﻿
@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<LatestVideoItemModel>>  _pageModelCatche

    <CnGalWebSite.Components.Containers.MiniModeContainer>
    <section class="latest-videoes-card">
        <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup Title="最新视频" Items="_items.Take(_dataCacheService.IsApp?2:4)" MoreLink="/search/?Types=Video">
            <div class="group">
                @foreach (var item in context)
                {
                    <MDCard Class="item" Href="@item.Url">
                        <img class="image" src="@item.Image" alt="@item.Name" loading="lazy" />
                        <div class="head">
                            <div class="content">
                                <div class="name text-truncate-2">@item.Name</div>
                            </div>
                            <div class="addinfo">
                                <div class="name text-truncate-1  label-large">@item.OriginalAuthor</div>
                                <div class="time text-truncate-1  label-large">@item.PublishTime</div>
                            </div>
                        </div>
                    </MDCard>
                }

            </div>
        </CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup>
    </section>

    </CnGalWebSite.Components.Containers.MiniModeContainer>

@code {

    List<IEnumerable<LatestVideoItemModel>> _items { get; set; } = new List<IEnumerable<LatestVideoItemModel>>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取主页信息
            var items = await _pageModelCatche.GetCache("api/home/ListLatestVideos");

            var i = 0;
            _items.Clear();
            while (i < items.Count)
            {
                _items.Add(items.Skip(i).Take(3));
                i += 3;
            }

            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页最新视频列表失败");
        }
    }
}
